﻿//-----------------------------------------------------------------------
// <copyright file="DancesController.cs" company="Christian Coda">
// Copyright (c) Christian Coda. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DancingSite.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using DancingSite.Models;

    /// <summary>
    /// The Dances controller class.
    /// </summary>
    public class DancesController : Controller
    {
        /// <summary>
        /// the database context
        /// </summary>
        private DanceDBContext db = new DanceDBContext();

        /// <summary>
        /// GET: /Dances/
        /// </summary>
        /// <returns>The index page.</returns>
        public ActionResult Index()
        {
            return this.View(this.db.Dances.ToList());
        }

        /// <summary>
        /// GET: /Dances/Details/5
        /// </summary>
        /// <param name="id">The id of the dance.</param>
        /// <returns>The details page.</returns>
        public ActionResult Details(int id = 0)
        {
            Dance dance = this.db.Dances.Find(id);
            if (dance == null)
            {
                return this.HttpNotFound();
            }

            return this.View(dance);
        }

        /// <summary>
        /// GET: /Dances/Create
        /// </summary>
        /// <returns>The create page.</returns>
        public ActionResult Create()
        {
            return this.View();
        }

        /// <summary>
        /// POST: /Dances/Create
        /// </summary>
        /// <param name="dance">The dance to create</param>
        /// <returns>The create page.</returns>
        [HttpPost]
        public ActionResult Create(Dance dance)
        {
            if (ModelState.IsValid)
            {
                this.db.Dances.Add(dance);
                this.db.SaveChanges();
                return this.RedirectToAction("Index");
            }

            return this.View(dance);
        }

        /// <summary>
        /// GET: /Dances/Edit/5
        /// </summary>
        /// <param name="id">The id of the dance.</param>
        /// <returns>The edit page.</returns>
        public ActionResult Edit(int id = 0)
        {
            Dance dance = this.db.Dances.Find(id);
            if (dance == null)
            {
                return this.HttpNotFound();
            }

            return this.View(dance);
        }

        /// <summary>
        /// POST: /Dances/Edit/5
        /// </summary>
        /// <param name="dance">The dance to edit.</param>
        /// <returns>The edit page.</returns>
        [HttpPost]
        public ActionResult Edit(Dance dance)
        {
            if (ModelState.IsValid)
            {
                this.db.Entry(dance).State = EntityState.Modified;
                this.db.SaveChanges();
                return this.RedirectToAction("Index");
            }

            return this.View(dance);
        }

        /// <summary>
        /// GET: /Dances/Delete/5
        /// </summary>
        /// <param name="id">The id of the dance</param>
        /// <returns>The delete page.</returns>
        public ActionResult Delete(int id = 0)
        {
            Dance dance = this.db.Dances.Find(id);
            if (dance == null)
            {
                return this.HttpNotFound();
            }

            return this.View(dance);
        }

        /// <summary>
        /// POST: /Dances/Delete/5
        /// </summary>
        /// <param name="id">The id of the dance.</param>
        /// <returns>The delete page.</returns>
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Dance dance = this.db.Dances.Find(id);
            if (null == dance)
            {
                return this.HttpNotFound();
            }

            this.db.Dances.Remove(dance);
            this.db.SaveChanges();
            return this.RedirectToAction("Index");
        }

        /// <summary>
        /// Dispose of this class instance.
        /// </summary>
        /// <param name="disposing">Dispose of managed resources.</param>
        protected override void Dispose(bool disposing)
        {
            this.db.Dispose();
            base.Dispose(disposing);
        }
    }
}